/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Reg_Magazine_Panel.java
 *
 * Created on Mar 24, 2010, 8:19:16 PM
 */

package library.GUI;

import SourceCode.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author zyang
 */
public class Reg_Magazine_Panel extends javax.swing.JPanel {

    /** Creates new form Reg_Magazine_Panel */
    public Reg_Magazine_Panel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        magazinelist = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        txttitle = new javax.swing.JTextField();
        txtissueno = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtDays = new javax.swing.JTextField();

        magazinelist.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Issue No.", "Title", "Chechout length", "Number Of Copy "
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(magazinelist);

        jButton3.setText("Serach");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("IssuNo");

        jButton1.setText("Add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Reset");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Title");

        jLabel5.setText("Days");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel3))
                                .addGap(27, 27, 27)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txttitle, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                                    .addComponent(txtissueno, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
                                .addGap(19, 19, 19)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton3)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtDays, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)))))
                .addGap(11, 11, 11))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtissueno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtDays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(txttitle, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void setMagazinelist()
    {
        Object[] rowdata;
       DefaultTableModel defTableModel = new DefaultTableModel();
       defTableModel.addColumn("IssueNo.");
       defTableModel.addColumn("Title");
       defTableModel.addColumn("Checkout Days");
       defTableModel.addColumn("Number Of Copy");
        for(Magazine m : LibraryGeneral.S_Magazines)
        {
            rowdata = new Object[]{m.getIssueno(),  m.getTitle(), m.getCheckoutlength(), m.Copies.size() };
            defTableModel.addRow(rowdata);

        }

        this.magazinelist.setModel(defTableModel);


    }
    public void setListByMagazineIssueNo(String IssueNo)
    {
       Object[] rowdata;
       DefaultTableModel defTableModel = new DefaultTableModel();
       defTableModel.addColumn("IssueNo.");
       defTableModel.addColumn("Title");
       defTableModel.addColumn("Checkout Days");
       defTableModel.addColumn("Number Of Copy");
       
       if(LibraryGeneral.Map_Magazines.containsKey(IssueNo) )
       {
        Book m = LibraryGeneral.Map_Books.get(IssueNo);
        rowdata = new Object[]{m.getISBN(),  m.getTitle(), m.getCheckoutlength(), m.getAuthor() , m.Copies.size()};
        defTableModel.addRow(rowdata);
        this.magazinelist.setModel(defTableModel);
       }
       else
       {
           JOptionPane.showMessageDialog(null, "No Valid Magazines");
           this.magazinelist.setModel(defTableModel);
       }

    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        String issueno = txtissueno.getText();
        if(issueno.length() > 0)
            setListByMagazineIssueNo(issueno);
        else
             setMagazinelist();
            //JOptionPane.showMessageDialog(null, "There is no Issue No to Search for");
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        int days ;
        try
        {
            if(txtissueno.getText().length() <=0 || txtDays.getText().length() <=0 || txttitle.getText().length() <=0)
            {
                JOptionPane.showMessageDialog(null, "Complete Missing Information ");
                return;
            }
            days = Integer.parseInt(txtDays.getText());
            LibraryGeneral.addMagazine(txttitle.getText(), days, txtissueno.getText());
            setMagazinelist();
            ResetText();
        }
        catch(Exception ex)
        {
            txtDays.setText("");
            JOptionPane.showMessageDialog(null, "invalid number of checkout days ");

        }
    }//GEN-LAST:event_jButton1ActionPerformed

    //reset all fields
    public void ResetText()
    {
        txttitle.setText("");
        txtissueno.setText("");
        txtDays.setText("");
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        ResetText();
}//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable magazinelist;
    private javax.swing.JTextField txtDays;
    private javax.swing.JTextField txtissueno;
    private javax.swing.JTextField txttitle;
    // End of variables declaration//GEN-END:variables

}
